from sklearn import linear_model, datasets
import matplotlib.pyplot as plt
import numpy as np

# datasets
np.random.seed(0)
dataX = 2 * np.random.rand(100, 1)
dataY = 2 + 5 * dataX + np.random.randn(100, 1)

x = dataX[:-1]
y = dataY[:-1]

# model
model = linear_model.LinearRegression()

# train
model.fit(x, y)
score = model.score(x, y)
print(f'斜率w: {model.coef_[0][0]}, 截距b: {model.intercept_[0]}, 得分: {score}')

y_pred = model.predict(x)

# plt
plt.scatter(x, y)
plt.plot(x, y_pred, color = 'red')
plt.title('Linear Regression Fit')
plt.show()

# predict
y_pred = model.predict([dataX[-1]])
y_true = dataY[-1]
print(f'y_pred={y_pred}, y_true={y_true}')